package com.liuweiheng.base.utils;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 全局 Redis工具类
 * @author lwh
 * @since 2021/5/6
 */
@Component
public class GlobalRedisUtil {
    private static RedisTemplate<String, Object> redisTemplate;

    static {
        redisTemplate = (RedisTemplate<String, Object>) ApplicationContextUtil.getBean("redisTemplate");
    }

    public static final long DEFAULT_EXPIRE = 864000L;
    public static final long NOT_EXPIRE = -1L;

    /**
     * 存储 key-value 键值对
     */
    public static boolean setVal(String key, Object val) {
        try {
         redisTemplate.opsForValue().set(key, val, DEFAULT_EXPIRE, TimeUnit.SECONDS);
        } catch (Exception e) {
            return false;
        }
        return true;
    }


}
